package csse3004.GD.PM;

/**
 * @author David Bullock s4206222
 */
import csse3004.GD.DA.PollMasterDA;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.view.RedirectView;

public class MessageController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log log = LogFactory.getLog(getClass());
    private PollMasterService service = new PollMasterDA();

    public ModelAndView onSubmit(Object command, HttpServletRequest req) throws ServletException {
        Message message = (Message) command;
        String body = message.getMessage();
        String title = message.getMessage();
        String type = message.getType();
        String uid = message.getUid();
        try{ if (type.equals("private")) {
            int userID = Integer.parseInt((String) req.getSession().getAttribute("userID"));
            service.sendMessage(true, userID, Integer.parseInt(uid), body, title);
        } else if (type.equals("public")) {
            log.info("public");
            service.sendMessage(true, 2, Integer.parseInt(uid), body, title);
        }
        } catch (Exception e){
            logger.info(e);
        }
        return new ModelAndView(new RedirectView(getSuccessView()));

    }
}